<?php
/**
 * Created by PhpStorm.
 * User: Victor
 * Date: 2018/9/14
 * Time: 15:36
 */

namespace app\manage\controller;


use think\Db;
use think\Request;
class Heyue extends BaseController
{
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        header("Access-Control-Allow-Origin: *");
        header("Access-Control-Allow-Methods:POST,GET");
        header("Access-Control-Allow-Headers:x-requested-with,content-type");
        header("Content-type:text/json;charset=utf-8");
    }
    public function index()
    {
        $data = input();
        if(empty($data['token'])){
            $result['code']=0;
            $result['msg']=urlencode('非法访问！');
            return urldecode(json_encode($result));
        }
        $name = base64_decode($data['token']);
        $token_check = db('guanli')
            ->where('bh',$name)
            ->find();
        if(count($token_check)>0){
            //$shuju = Db::query('SELECT *,(SELECT name FROM heyue_zu WHERE heyue.heyue_zu_id = heyue_zu.id) as zu_name, FROM_UNIXTIME(youxiao_shijian) as daoqi_time FROM heyue');
            $shuju = Db::query('SELECT *,(SELECT name FROM heyue_zu WHERE heyue.heyue_zu_id = heyue_zu.id) as zu_name FROM heyue');
            foreach ($shuju as $key => $value){
                if($value['xs_bz']==1){
                    $shuju[$key]['xs_bz'] = '是';
                }else{
                    $shuju[$key]['xs_bz'] = '否';
                }
                $shuju[$key]['daoqi_time'] = date("Y-m-d H:i:s", $value['youxiao_shijian']);
            }
            $result['code']=1;
            $result['shuju']= $shuju;
            $result['msg']=urlencode('请求数据成功');
            return urldecode(json_encode($result));
        }else{
            $result['code']=0;
            $result['msg']=urlencode('该用户不存在！');
            return urldecode(json_encode($result));
        }
    }
    public function heyue_add_shuju()
    {
        $heyue_zu = db('heyue_zu')
            ->field('id,name')
            ->select();
        $jiaoyi = array(
            array("是",1),
            array("否",0));
        $result['code']=1;
        $result['shuju']= array($heyue_zu,$jiaoyi);
        $result['msg']=urlencode('666');
        return urldecode(json_encode($result));
    }
    public function heyue_add_or_edit()
    {
        $data = input();
        $data = collection($data)->toArray();
        if(empty($data['token'])){
            $result['code']=0;
            $result['msg']=urlencode('非法访问！');
            return urldecode(json_encode($result));
        }
        $name = base64_decode($data['token']);
        $token_check = db('guanli')
            ->where('bh',$name)
            ->find();
        if(count($token_check)>0){
            $detail = $data['shuju'];
            if($detail['youxiao_shijian']=='0:00:00'){
                $detail['youxiao_shijian'] = 0;
            }else{
                $detail['youxiao_shijian'] =  strtotime($detail['youxiao_shijian']);
            }

            //dump($data_detail);die();
            $heyue = model('Heyue');
            if(empty($detail['id'])){
                $is_have = db('heyue')
                    ->where(['bh'=>$detail['bh'],'heyue_zu_id'=>$detail['heyue_zu_id']])
                    ->find();
                if($is_have){
                    $result['code']=0;
                    $result['msg']=urlencode('已有相同的合约！');
                    return urldecode(json_encode($result));
                }
                $heyue->data($detail);
                $res = $heyue -> allowField(true) -> save();
            }else{
                $is_have = db('heyue')
                    ->where(['bh'=>$detail['bh'],'heyue_zu_id'=>$detail['heyue_zu_id']])
                    ->where('id','neq',$detail['id'])
                    ->find();
                if($is_have){
                    $result['code']=0;
                    $result['msg']=urlencode('已有相同的合约！');
                    return urldecode(json_encode($result));
                }
                $res = $heyue -> allowField(true)->save($detail,['id' => $detail['id']]);
            }

            if($res>0){
                $redis = $this -> redis;
                $re_name = 'update_bz';
                $re_value = '1';
                $redis->set($re_name,$re_value);
                $result['code']=1;
                $result['msg']=urlencode('提交成功');
                return urldecode(json_encode($result));
            }else{
                $result['code']=0;
                $result['msg']=urlencode('未做数据修改，请检查后再次提交！');
                return urldecode(json_encode($result));
            }
        }else{
            $result['code']=0;
            $result['msg']=urlencode('用户不存在！');
            return urldecode(json_encode($result));
        }
    }
    public function heyue_delete()
    {
        $data = input();
        //dump($data);die();
        $data = collection($data)->toArray();
        if(empty($data['token']) or empty($data['id'])){
            $result['code']=0;
            $result['msg']=urlencode('非法访问！');
            return urldecode(json_encode($result));
        }
        $name = base64_decode($data['token']);
        $token_check = db('guanli')
            ->where('bh',$name)
            ->find();
        if(count($token_check)>0){
            $res = db('heyue')
                ->where('id',$data['id'])
                ->delete();
            if($res>0){
                $redis = $this -> redis;
                $re_name = 'update_bz';
                $re_value = '1';
                $redis->set($re_name,$re_value);
                $result['code']=1;
                $result['msg']=urlencode('删除成功');
                return urldecode(json_encode($result));
            }else{
                $result['code']=0;
                $result['msg']=urlencode('删除失败，请重试！');
                return urldecode(json_encode($result));
            }
        }else{
            $result['code']=0;
            $result['msg']=urlencode('用户不存在！');
            return urldecode(json_encode($result));
        }
    }

}